module scene {
    export class SuccessScene extends eui.Component {

        private maskBg: eui.Image
        private successLevelText: eui.Label
        private successPercent: eui.Label
        private successScoreText: eui.Label
        private successNextText: eui.Label
        private lightMovie: egret.tween.TweenGroup
        private btnMovie: egret.tween.TweenGroup
        private successDoubleBtn: eui.Image
        private successBtnMovie: eui.Image
        private maskImg: eui.Image
        private timer
        private addScore: number = 200

        private score: number
        private level: number

        public constructor(score, level) {
            super();
            this.score = score
            this.level = level

            this.addEventListener(eui.UIEvent.COMPLETE, this.onComplete, this);
            this.skinName = "SuccessPageSkin";
            
        }

        public onComplete(eve: eui.UIEvent): void {
            manager.StatisticsManager.sceneCount(1006)
            console.log('SuccessScene')
            this.maskBg.width = manager.DisplayManager.STAGE_WIDTH;
            this.maskBg.height = manager.DisplayManager.STAGE_HEIGHT;

            this.successNextText.visible = false
            setTimeout(() => {
                this.successNextText.visible = true
            }, 500)

            this.successDoubleBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onDouble, this);

            this.successNextText.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onNextLevel, this);

            this.successLevelText.text = `${this.level}`

            this.successScoreText.text = `${this.score}`

            this.addScore = 200

            let random = Math.random() * 10

            let percent = (this.level - 1) * 7 + random + 1
            if(percent >= 98) {
                percent = 98
            }

            this.successBtnMovie.mask = this.maskImg

            this.successPercent.text  =`已经超越${Math.floor(percent)}%的用户`
            this.lightMovie.play(0)   
            this.btnMovie.play(0)                     
            this.timer = setInterval(() => {
                console.log('setInterval')
                this.lightMovie.play(0)
                this.btnMovie.play(0)
            }, 3000)
        }

        private onDouble() {
            manager.StatisticsManager.click(153)
            let adManager = manager.GameManager.getInstance().getAdManager();
            let revivalMethod = Number(adManager.ad_percent) - Math.random() * 100
            if(revivalMethod >= 0){
                adManager.showVideoAd();
                adManager.addEventListener(manager.AdManager.VIDEO_AD_CLOSE,
                    this.onVideoAdClose, this);
            }else{
                manager.ShareManager.getInstance().addEventListener(manager.ShareManager.SHARE_STATUS,
                    this.onShareStatus, this);
                manager.GameManager.getInstance().commonShare();
            }

            // this.onShareSucc();            
            
        }

        private onShareStatus(eve: egret.Event): void {
            manager.ShareManager.getInstance().removeEventListener(manager.ShareManager.SHARE_STATUS,
                this.onShareStatus, this);
            let succ: number = eve.data;
            if (succ > 0) {
                this.onShareSucc();
            }
        }

        private onVideoAdClose(eve: egret.Event): void {
            egret.log("视频广告播放事件:", eve.data);
            if (eve.data != null && eve.data.isEnded) {
                manager.GameManager.getInstance().adManager.removeEventListener(manager.AdManager.VIDEO_AD_CLOSE,
                    this.onVideoAdClose, this);
                this.onShareSucc();
            }
        }

        private onShareSucc(): void {
            this.addScore = 400
            clearInterval(this.timer)
            let event: events.NormalGameLevelPassedEvent = new events.NormalGameLevelPassedEvent(this.level + 1, this.addScore);
            this.dispatchEvent(event)
        }



        private onNextLevel() {
            manager.StatisticsManager.click(154)
            clearInterval(this.timer)
            let event: events.NormalGameLevelPassedEvent = new events.NormalGameLevelPassedEvent(this.level + 1, this.addScore);
            this.dispatchEvent(event)
        }
    }
}